package com.ruoyi.web.controller.research;

import java.util.List;

import com.ruoyi.domain.TycPatentZd;
import com.ruoyi.domain.TycResearch;
import com.ruoyi.domain.TycResearchZd;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.ITycResearchService;
import com.ruoyi.service.ITycResearchZdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2020-09-14
 */
@Controller
@RequestMapping("/ruoyi/research")
public class TycResearchController extends BaseController
{
    private String prefix = "ruoyi/research";

    @Autowired
    private ITycResearchService tycResearchService;
    @Autowired
    private ITycResearchZdService iTycResearchZdService;

    @RequiresPermissions("ruoyi:research:view")
    @GetMapping()
    public String research()
    {
        return prefix + "/research";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("ruoyi:research:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TycResearch tycResearch)
    {
        startPage();
        List<TycResearch> list = tycResearchService.selectTycResearchList(tycResearch);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("ruoyi:research:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TycResearch tycResearch)
    {
        List<TycResearch> list = tycResearchService.selectTycResearchList(tycResearch);
        ExcelUtil<TycResearch> util = new ExcelUtil<TycResearch>(TycResearch.class);
        return util.exportExcel(list, "research");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("ruoyi:research:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TycResearch tycResearch)
    {
        //查询字典赋予 积分 奖金 数据
        tycResearch.setGrade(iTycResearchZdService.selectTycResearchZdGrade(tycResearch.getAclass()).getAward());
        tycResearch.setBonus(iTycResearchZdService.selectTycResearchZdGrade(tycResearch.getAclass()).getScore());
        //获取当前登录用户信息存入上传人
        tycResearch.setApplyn(ShiroUtils.getLoginName());
        tycResearch.setUserId(ShiroUtils.getUserId());
        return toAjax(tycResearchService.insertTycResearch(tycResearch));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TycResearch tycResearch = tycResearchService.selectTycResearchById(id);
        mmap.put("tycResearch", tycResearch);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("ruoyi:research:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TycResearch tycResearch)
    {
        return toAjax(tycResearchService.updateTycResearch(tycResearch));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("ruoyi:research:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tycResearchService.deleteTycResearchByIds(ids));
    }
    /**
     * 查询课题字典表
     *
     * @param tycResearchZd
     * @return
     */
    @RequiresPermissions("ruoyi:research:score")
    @GetMapping("/score")
    @ResponseBody
    public List<TycResearchZd> plans(TycResearchZd tycResearchZd) {
        //查询课题字典表所有的属性
        List<TycResearchZd> ResearchZd = iTycResearchZdService.selectTycResearchZdList(tycResearchZd);
        return ResearchZd;

    }
    /**
     * 提交申请备案
     */
    @RequiresPermissions("ruoyi:research:submitApply")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long id) {
        //更新申报状态为待备案
        tycResearchService.submitApplications(id);
        return success();
    }


    /**
     * 提交申请
     */
    @RequiresPermissions("ruoyi:research:submitForApproval")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitForApproval")
    @ResponseBody
    public AjaxResult submitForApproval(Long id) {
        //更新审批状态为待审批
        tycResearchService.updateSubmitForApproval(id);
        return success();
    }

    /**
     * 撤销申请
     */
    @RequiresPermissions("ruoyi:research:revocation")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/revocation")
    @ResponseBody
    public AjaxResult revocation(Long id) {
        //更新审批状态为未备案
        tycResearchService.withdrawClaim(id);
        return success();
    }
}
